<!DOCTYPE html>
<html lang=en>
<head>
  <meta charset="utf-8">
  
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="renderer" content="webkit">
  <meta http-equiv="Cache-Control" content="no-transform" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="format-detection" content="telephone=no,email=no,adress=no">
  <!-- Color theme for statusbar -->
  <meta name="theme-color" content="#000000" />
  <!-- 强制页面在当前窗口以独立页面显示,防止别人在框架里调用页面 -->
  <meta http-equiv="window-target" content="_top" />
  
  
  <title>MobileNetv1v2详解 | 鲨鱼之家</title>
  <meta name="description" content="MobileNetV1  简介 核心 亮点 DW卷积详解 PW卷积详解 DW卷积和PW卷积配合使用，实现深度可分卷积 参数数量对比 缺点   MobileNetV2  简介 亮点 倒残差结构详解 Linear Bottlenecks详解 与resnet结合：shortcut MobileNetV2具体结构   性能对比 MobileNetV2代码实战   MobileNetV1 简介 Mobi">
<meta property="og:type" content="article">
<meta property="og:title" content="MobileNetv1v2详解">
<meta property="og:url" content="http://tina-yao.gitee.io/bigbig-shark/2022/09/27/MobileNet/index.html">
<meta property="og:site_name" content="大鲨鱼">
<meta property="og:description" content="MobileNetV1  简介 核心 亮点 DW卷积详解 PW卷积详解 DW卷积和PW卷积配合使用，实现深度可分卷积 参数数量对比 缺点   MobileNetV2  简介 亮点 倒残差结构详解 Linear Bottlenecks详解 与resnet结合：shortcut MobileNetV2具体结构   性能对比 MobileNetV2代码实战   MobileNetV1 简介 Mobi">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/4.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/5.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/6.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/1.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/7.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/8.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/9.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/10.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/11.png">
<meta property="og:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/12.png">
<meta property="article:published_time" content="2022-09-27T08:13:31.000Z">
<meta property="article:modified_time" content="2022-10-03T11:25:19.722Z">
<meta property="article:author" content="BigbigShark">
<meta property="article:tag" content="轻量级">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/4.png">
  <!-- Canonical links -->
  <link rel="canonical" href="http://tina-yao.gitee.io/bigbig-shark/2022/09/27/MobileNet/index.html">
  
    <link rel="alternate" href="/atom.xml" title="大鲨鱼" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png" type="image/x-icon">
  
  
<link rel="stylesheet" href="/bigbig-shark/css/style.css">

  
  
  
  
<meta name="generator" content="Hexo 5.4.0"></head>


<body class="main-center theme-purple# 主题颜色 theme-black theme-blue theme-green theme-purple" itemscope itemtype="http://schema.org/WebPage">
  <header class="header" itemscope itemtype="http://schema.org/WPHeader">
  <div class="slimContent">
    <div class="navbar-header">
      
      
      <div class="profile-block text-center">
        <a id="avatar" href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank">
          <img class="img-circle img-rotate" src="/bigbig-shark/images/avatar.jpg" width="200" height="200">
        </a>
        <h2 id="name" class="hidden-xs hidden-sm">大鲨鱼</h2>
        <h3 id="title" class="hidden-xs hidden-sm hidden-md">CV&amp;Robots</h3>
        <small id="location" class="text-muted hidden-xs hidden-sm"><i class="icon icon-map-marker"></i> Wuhan, China</small>
      </div>
      
      <div class="search" id="search-form-wrap">

    <form class="search-form sidebar-form">
        <div class="input-group">
            <input type="text" class="search-form-input form-control" placeholder="Search" />
            <span class="input-group-btn">
                <button type="submit" class="search-form-submit btn btn-flat" onclick="return false;"><i class="icon icon-search"></i></button>
            </span>
        </div>
    </form>
    <div class="ins-search">
  <div class="ins-search-mask"></div>
  <div class="ins-search-container">
    <div class="ins-input-wrapper">
      <input type="text" class="ins-search-input" placeholder="Type something..." x-webkit-speech />
      <button type="button" class="close ins-close ins-selectable" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
    </div>
    <div class="ins-section-wrapper">
      <div class="ins-section-container"></div>
    </div>
  </div>
</div>


</div>
      <button class="navbar-toggle collapsed" type="button" data-toggle="collapse" data-target="#main-navbar" aria-controls="main-navbar" aria-expanded="false">
        <span class="sr-only">Toggle navigation</span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
    </div>
    <nav id="main-navbar" class="collapse navbar-collapse" itemscope itemtype="http://schema.org/SiteNavigationElement" role="navigation">
      <ul class="nav navbar-nav main-nav menu-highlight">
        
        
        <li class="menu-item menu-item-home">
          <a href="/bigbig-shark/.">
            
            <i class="icon icon-home-fill"></i>
            
            <span class="menu-title">Home</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-archives">
          <a href="/bigbig-shark/archives">
            
            <i class="icon icon-archives-fill"></i>
            
            <span class="menu-title">Archives</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-categories">
          <a href="/bigbig-shark/categories">
            
            <i class="icon icon-folder"></i>
            
            <span class="menu-title">Categories</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-tags">
          <a href="/bigbig-shark/tags">
            
            <i class="icon icon-tags"></i>
            
            <span class="menu-title">Tags</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-links">
          <a href="/bigbig-shark/links">
            
            <i class="icon icon-friendship"></i>
            
            <span class="menu-title">Links</span>
          </a>
        </li>
        
        
        <li class="menu-item menu-item-about">
          <a href="/bigbig-shark/about">
            
            <i class="icon icon-cup-fill"></i>
            
            <span class="menu-title">About</span>
          </a>
        </li>
        
      </ul>
      
	
    <ul class="social-links">
    	
        <li><a href="https://gitee.com/tina-yao" target="_blank" title="Gitee" data-toggle=tooltip data-placement=top><i class="icon icon-gitee"></i></a></li>
        
    </ul>

    </nav>
  </div>
</header>

  
    <aside class="sidebar" itemscope itemtype="http://schema.org/WPSideBar">
  <div class="slimContent">
    
      <div class="widget">
    <h3 class="widget-title">Board</h3>
    <div class="widget-body">
        <div id="board">
            <div class="content">
                <p>欢迎交流与分享经验!</p>
            </div>
        </div>
    </div>
</div>

    
      
  <div class="widget">
    <h3 class="widget-title">Categories</h3>
    <div class="widget-body">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/AI/">AI</a><span class="category-list-count">1</span><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/AI/ML/">ML</a><span class="category-list-count">1</span></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/Concepts/">Concepts</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/Robots/">Robots</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E5%8D%8A%E6%97%A5%E9%97%B2/">半日闲</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E6%95%B0%E5%AD%A6%E5%BB%BA%E6%A8%A1/">数学建模</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90/">数据分析</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/bigbig-shark/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89/">计算机视觉</a><span class="category-list-count">30</span></li></ul>
    </div>
  </div>


    
      
  <div class="widget">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget-body tagcloud">
      <a href="/bigbig-shark/tags/CNN-backbones/" style="font-size: 13.67px;">CNN_backbones</a> <a href="/bigbig-shark/tags/OpenCV/" style="font-size: 13px;">OpenCV</a> <a href="/bigbig-shark/tags/Python/" style="font-size: 13px;">Python</a> <a href="/bigbig-shark/tags/ROS/" style="font-size: 13px;">ROS</a> <a href="/bigbig-shark/tags/%E4%BB%A3%E7%A0%81/" style="font-size: 13px;">代码</a> <a href="/bigbig-shark/tags/%E5%85%83%E5%AE%87%E5%AE%99/" style="font-size: 13px;">元宇宙</a> <a href="/bigbig-shark/tags/%E5%85%B4%E8%B6%A3/" style="font-size: 13px;">兴趣</a> <a href="/bigbig-shark/tags/%E5%9C%BA%E6%99%AF%E6%96%87%E5%AD%97%E8%AF%86%E5%88%AB/" style="font-size: 13px;">场景文字识别</a> <a href="/bigbig-shark/tags/%E6%91%98%E6%8A%84/" style="font-size: 13.33px;">摘抄</a> <a href="/bigbig-shark/tags/%E6%95%B0%E6%8D%AE%E9%9B%86/" style="font-size: 13px;">数据集</a> <a href="/bigbig-shark/tags/%E7%9B%AE%E6%A0%87%E6%A3%80%E6%B5%8B/" style="font-size: 14px;">目标检测</a> <a href="/bigbig-shark/tags/%E7%BE%8E%E8%B5%9B/" style="font-size: 13px;">美赛</a> <a href="/bigbig-shark/tags/%E8%81%9A%E7%B1%BB/" style="font-size: 13px;">聚类</a> <a href="/bigbig-shark/tags/%E8%87%AA%E5%8A%A8%E9%A9%BE%E9%A9%B6/" style="font-size: 13.33px;">自动驾驶</a> <a href="/bigbig-shark/tags/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%893D/" style="font-size: 13px;">计算机视觉3D</a> <a href="/bigbig-shark/tags/%E8%BD%BB%E9%87%8F%E7%BA%A7/" style="font-size: 13px;">轻量级</a>
    </div>
  </div>

    
  </div>
</aside>

  
  
<main class="main" role="main">
  <div class="content">
  <article id="post-MobileNet" class="article article-type-post" itemscope itemtype="http://schema.org/BlogPosting">
    
    <div class="article-header">
      
        
  
    <h1 class="article-title" itemprop="name">
      MobileNetv1v2详解
    </h1>
  

      
      <div class="article-meta">
        <span class="article-date">
    <i class="icon icon-calendar-check"></i>
	<a href="/bigbig-shark/2022/09/27/MobileNet/" class="article-date">
	  <time datetime="2022-09-27T08:13:31.000Z" itemprop="datePublished">2022-09-27</time>
	</a>
</span>
        
  <span class="article-category">
    <i class="icon icon-folder"></i>
    <a class="article-category-link" href="/bigbig-shark/categories/%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%86%E8%A7%89/">计算机视觉</a>
  </span>

        
  <span class="article-tag">
    <i class="icon icon-tags"></i>
	<a class="article-tag-link-link" href="/bigbig-shark/tags/%E8%BD%BB%E9%87%8F%E7%BA%A7/" rel="tag">轻量级</a>
  </span>


        

	<span class="article-read hidden-xs">
    	<i class="icon icon-eye-fill" aria-hidden="true"></i>
    	<span id="/bigbig-shark/2022/09/27/MobileNet/" class="leancloud_visitors"  data-flag-title="MobileNetv1v2详解">
			<span class="leancloud-visitors-count">0</span>
		</span>
    </span>

        <span class="post-comment"><i class="icon icon-comment"></i> <a href="/bigbig-shark/2022/09/27/MobileNet/#comments" class="article-comment-link">Comments</a></span>
        
	
		<span class="post-wordcount hidden-xs" itemprop="wordCount">Word Count: 1k(words)</span>
	
	
		<span class="post-readcount hidden-xs" itemprop="timeRequired">Read Count: 4(minutes)</span>
	

      </div>
    </div>
    <div class="article-entry marked-body" itemprop="articleBody">
      
        <!-- toc -->
<ul>
<li><a href="#mobilenetv1">MobileNetV1</a>
<ul>
<li><a href="#%E7%AE%80%E4%BB%8B">简介</a></li>
<li><a href="#%E6%A0%B8%E5%BF%83">核心</a></li>
<li><a href="#%E4%BA%AE%E7%82%B9">亮点</a></li>
<li><a href="#dw%E5%8D%B7%E7%A7%AF%E8%AF%A6%E8%A7%A3">DW卷积详解</a></li>
<li><a href="#pw%E5%8D%B7%E7%A7%AF%E8%AF%A6%E8%A7%A3">PW卷积详解</a></li>
<li><a href="#dw%E5%8D%B7%E7%A7%AF%E5%92%8Cpw%E5%8D%B7%E7%A7%AF%E9%85%8D%E5%90%88%E4%BD%BF%E7%94%A8%E5%AE%9E%E7%8E%B0%E6%B7%B1%E5%BA%A6%E5%8F%AF%E5%88%86%E5%8D%B7%E7%A7%AF">DW卷积和PW卷积配合使用，实现深度可分卷积</a></li>
<li><a href="#%E5%8F%82%E6%95%B0%E6%95%B0%E9%87%8F%E5%AF%B9%E6%AF%94">参数数量对比</a></li>
<li><a href="#%E7%BC%BA%E7%82%B9">缺点</a></li>
</ul>
</li>
<li><a href="#mobilenetv2">MobileNetV2</a>
<ul>
<li><a href="#%E7%AE%80%E4%BB%8B-1">简介</a></li>
<li><a href="#%E4%BA%AE%E7%82%B9-1">亮点</a></li>
<li><a href="#%E5%80%92%E6%AE%8B%E5%B7%AE%E7%BB%93%E6%9E%84%E8%AF%A6%E8%A7%A3">倒残差结构详解</a></li>
<li><a href="#linear-bottlenecks%E8%AF%A6%E8%A7%A3">Linear Bottlenecks详解</a></li>
<li><a href="#%E4%B8%8Eresnet%E7%BB%93%E5%90%88shortcut">与resnet结合：shortcut</a></li>
<li><a href="#mobilenetv2%E5%85%B7%E4%BD%93%E7%BB%93%E6%9E%84">MobileNetV2具体结构</a></li>
</ul>
</li>
<li><a href="#%E6%80%A7%E8%83%BD%E5%AF%B9%E6%AF%94">性能对比</a></li>
<li><a href="#mobilenetv2%E4%BB%A3%E7%A0%81%E5%AE%9E%E6%88%98">MobileNetV2代码实战</a></li>
</ul>
<!-- tocstop -->
<h2><span id="mobilenetv1">MobileNetV1</span></h2>
<h3><span id="简介">简介</span></h3>
<p>MobileNet网络是由google团队在2017年提出的，专注于移动端或者嵌入式设备中的轻量级CNN网络。相比传统卷积神经网络，在准确率小幅下降的情况下，大大减少了模型参数与运算量。相比VGG16，准确率下降0.9%，但模型参数只有VGG16的1/32。</p>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/4.png" alt></p>
<h3><span id="核心">核心</span></h3>
<ul>
<li>深度可分卷积Depth-wise Separable Convolution</li>
</ul>
<h3><span id="亮点">亮点</span></h3>
<ul>
<li>增加了超参数$\alpha、\beta$
<ul>
<li>$\alpha$: Width Multiplier，是卷积核个数的倍率，用来控制卷积核个数</li>
<li>$\beta$: Resolution Multiplier，控制输入图像的大小</li>
</ul>
</li>
</ul>
<h3><span id="dw卷积详解">DW卷积详解</span></h3>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/5.png" alt></p>
<h3><span id="pw卷积详解">PW卷积详解</span></h3>
<p>和普通卷积一样，只是说卷积核大小为$1\times1$</p>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/6.png" alt></p>
<h3><span id="dw卷积和pw卷积配合使用实现深度可分卷积">DW卷积和PW卷积配合使用，实现深度可分卷积</span></h3>
<h3><span id="参数数量对比">参数数量对比</span></h3>
<p>（注意，计算量应该用输出的去算，即这个DF应该是灰色输出特征的大小；但此处认为输出和输入的大小相同，所以可以用输入去算，本质上还是在用输出去算）</p>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/1.png" alt></p>
<h3><span id="缺点">缺点</span></h3>
<p>实际训练过程中发现很多DW卷积核参数为0，废掉了，为了解决这个问题。提出了MobileNetv2</p>
<h2><span id="mobilenetv2">MobileNetV2</span></h2>
<h3><span id="简介">简介</span></h3>
<p>MobileNetv2是由google团队在2018年提出的，相比MobileNetv1，准确率更高，模型更小。</p>
<blockquote>
<p>MobileNetV2: Inverted Residuals and Linear Bottlenecks</p>
</blockquote>
<h3><span id="亮点">亮点</span></h3>
<ul>
<li>Inverted Residuals倒残差结构</li>
<li>Linear Bottlenecks</li>
</ul>
<h3><span id="倒残差结构详解">倒残差结构详解</span></h3>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/7.png" alt></p>
<p>注意，倒残差结构使用的是：</p>
<ul>
<li>
<p>relu6</p>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/8.png" alt></p>
</li>
<li>
<p>DW Conv</p>
</li>
</ul>
<h3><span id="linear-bottlenecks详解">Linear Bottlenecks详解</span></h3>
<p>即针对倒残差结构的最后一个$1\times1$的卷积层，使用的不是ReLU6，而是一个线性激活函数，将该操作成为Linear Bottlenecks。</p>
<p>理论依据：</p>
<ol>
<li>首先用一个矩阵T将低维特征映射到更高维空间；</li>
<li>然后在该高维空间中使用ReLU激活函数，得到新的特征；</li>
<li>将该新的特征用矩阵T的逆矩阵映射会原本的低维空间，发现在高维空间中使用非线性激活函数ReLU容易丢失低维特征信息，具体结果如下图所示。（红框内的话表述不太准确）</li>
</ol>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/9.png" alt></p>
<h3><span id="与resnet结合shortcut">与resnet结合：shortcut</span></h3>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/10.png" alt></p>
<h3><span id="mobilenetv2具体结构">MobileNetV2具体结构</span></h3>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/11.png" alt></p>
<h2><span id="性能对比">性能对比</span></h2>
<p><img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/imgs/MobileNet/12.png" alt></p>
<h2><span id="mobilenetv2代码实战">MobileNetV2代码实战</span></h2>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> torch</span><br><span class="line"><span class="keyword">import</span> torch.nn <span class="keyword">as</span> nn</span><br><span class="line"><span class="keyword">import</span> torch.nn.functional <span class="keyword">as</span> F</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">InvertedResidual</span>(<span class="params">nn.Module</span>):</span></span><br><span class="line">  <span class="comment"># t: expansion factor</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, in_channel, out_channel, stride, t=<span class="number">6</span>, class_num=<span class="number">5</span></span>):</span></span><br><span class="line">    <span class="built_in">super</span>().__init__()</span><br><span class="line"></span><br><span class="line">    hidden_channel = in_channel * t</span><br><span class="line">    self.inverted_residual = nn.Sequential(</span><br><span class="line">      <span class="comment"># Elevate Dimension: PW Conv</span></span><br><span class="line">      <span class="comment"># groups: Number of blocked connections from input channels to output channels. Default: 1</span></span><br><span class="line">      <span class="comment"># bias: If True, adds a learnable bias to the output. Default: True</span></span><br><span class="line">      nn.Conv2d(in_channels=in_channel, out_channels=hidden_channel, stride=<span class="number">1</span>, kernel_size=<span class="number">1</span>),</span><br><span class="line">      nn.BatchNorm2d(hidden_channel),</span><br><span class="line">      nn.ReLU6(inplace=<span class="literal">True</span>), <span class="comment"># inplace=True means to change the original inputs</span></span><br><span class="line"></span><br><span class="line">      <span class="comment"># DW Conv</span></span><br><span class="line">      <span class="comment"># groups=in_channel * t=hidden_channel means to do depth-wise convolution</span></span><br><span class="line">      nn.Conv2d(hidden_channel, hidden_channel, kernel_size=<span class="number">3</span>, stride=stride, padding=<span class="number">1</span>, groups=hidden_channel),</span><br><span class="line">      nn.BatchNorm2d(hidden_channel),</span><br><span class="line">      nn.ReLU6(inplace=<span class="literal">True</span>),</span><br><span class="line"></span><br><span class="line">      <span class="comment"># Reduce Dimension</span></span><br><span class="line">      nn.Conv2d(hidden_channel, out_channel, kernel_size=<span class="number">1</span>),</span><br><span class="line">      nn.BatchNorm2d(out_channel),</span><br><span class="line">      <span class="comment"># Linear Bottleneck</span></span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    self.stride = stride</span><br><span class="line">    self.in_channel = in_channel</span><br><span class="line">    self.out_channel = out_channel</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">forward</span>(<span class="params">self, x</span>):</span></span><br><span class="line">    inverted_residual = self.inverted_residual(x)</span><br><span class="line">    <span class="keyword">if</span> self.stride == <span class="number">1</span> <span class="keyword">and</span> self.in_channel == self.out_channel:</span><br><span class="line">      inverted_residual += x</span><br><span class="line">    <span class="keyword">return</span> inverted_residual</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">MobileNetV2</span>(<span class="params">nn.Module</span>):</span></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self, class_num=<span class="number">5</span></span>):</span></span><br><span class="line">    <span class="built_in">super</span>().__init__()</span><br><span class="line"></span><br><span class="line">    self.pre = nn.Sequential(</span><br><span class="line">      nn.Conv2d(<span class="number">3</span>, <span class="number">32</span>, kernel_size=<span class="number">1</span>, stride=<span class="number">2</span>),</span><br><span class="line">      nn.BatchNorm2d(<span class="number">32</span>),</span><br><span class="line">      nn.ReLU6(inplace=<span class="literal">True</span>),</span><br><span class="line">    )</span><br><span class="line"></span><br><span class="line">    <span class="comment"># in_channel, out_channel, stride, t</span></span><br><span class="line">    self.layer1 = InvertedResidual(<span class="number">32</span>, <span class="number">16</span>, <span class="number">1</span>, <span class="number">1</span>)</span><br><span class="line">    <span class="comment"># n, in_channel, out_channel, stride, t</span></span><br><span class="line">    <span class="comment"># stride: the module&#x27;s 1st layer&#x27;s stride, and the others are all 1</span></span><br><span class="line">    self.layer2 = self._make_layer(<span class="number">2</span>, <span class="number">16</span>, <span class="number">24</span>, <span class="number">2</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer3 = self._make_layer(<span class="number">3</span>, <span class="number">24</span>, <span class="number">32</span>, <span class="number">2</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer4 = self._make_layer(<span class="number">4</span>, <span class="number">32</span>, <span class="number">64</span>, <span class="number">2</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer5 = self._make_layer(<span class="number">3</span>, <span class="number">64</span>, <span class="number">96</span>, <span class="number">1</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer6 = self._make_layer(<span class="number">3</span>, <span class="number">96</span>, <span class="number">160</span>, <span class="number">1</span>, <span class="number">6</span>)</span><br><span class="line">    self.layer7 = InvertedResidual(<span class="number">160</span>, <span class="number">320</span>, <span class="number">1</span>, <span class="number">6</span>)</span><br><span class="line"></span><br><span class="line">    self.conv1 = nn.Sequential(</span><br><span class="line">      nn.Conv2d(<span class="number">320</span>, <span class="number">1280</span>, <span class="number">1</span>),</span><br><span class="line">      nn.BatchNorm2d(<span class="number">1280</span>),</span><br><span class="line">      nn.ReLU6(inplace=<span class="literal">True</span>),</span><br><span class="line">    )</span><br><span class="line">    self.conv2 = nn.Conv2d(<span class="number">1280</span>, class_num, <span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">forward</span>(<span class="params">self, x</span>):</span></span><br><span class="line">    x = self.pre(x)</span><br><span class="line">    x = self.layer1(x)</span><br><span class="line">    x = self.layer2(x)</span><br><span class="line">    x = self.layer3(x)</span><br><span class="line">    x = self.layer4(x)</span><br><span class="line">    x = self.layer5(x)</span><br><span class="line">    x = self.layer6(x)</span><br><span class="line">    x = self.layer7(x)</span><br><span class="line">    x = self.conv1(x)</span><br><span class="line">    x = F.adaptive_avg_pool2d(x, output_size=<span class="number">1</span>)</span><br><span class="line">    x = self.conv2(x)</span><br><span class="line">    x = x.view(x.size(<span class="number">0</span>), -<span class="number">1</span>)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> x</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">def</span> <span class="title">_make_layer</span>(<span class="params">self, repeat, in_channel, out_channel, stride, t</span>):</span></span><br><span class="line">    layers = []</span><br><span class="line">    layers.append(InvertedResidual(in_channel, out_channel, stride, t))</span><br><span class="line"></span><br><span class="line">    <span class="keyword">while</span> repeat - <span class="number">1</span>:</span><br><span class="line">      layers.append(InvertedResidual(out_channel, out_channel, <span class="number">1</span>, t))</span><br><span class="line">      repeat -= <span class="number">1</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> nn.Sequential(*layers)</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">mobilenetv2</span>():</span></span><br><span class="line">  <span class="keyword">return</span> MobileNetV2()</span><br></pre></td></tr></table></figure>
<hr>
<p>整理自B站<a target="_blank" rel="noopener" href="https://space.bilibili.com/18161609">霹雳吧啦Wz</a>，侵删。</p>

      
    </div>
    <div class="article-footer">
      <blockquote class="mt-2x">
  <ul class="post-copyright list-unstyled">
    
    <li class="post-copyright-link hidden-xs">
      <strong>本文链接：</strong>
      <a href="http://tina-yao.gitee.io/bigbig-shark/2022/09/27/MobileNet/" title="MobileNetv1v2详解" target="_blank" rel="external">http://tina-yao.gitee.io/bigbig-shark/2022/09/27/MobileNet/</a>
    </li>
    
    <li class="post-copyright-license">
      <strong>版权声明： </strong> 本博客所有文章除特别声明外，均采用 <a href="http://creativecommons.org/licenses/by/4.0/deed.zh" target="_blank" rel="external">CC BY 4.0 CN协议</a> 许可协议。转载请注明出处！
    </li>
  </ul>
</blockquote>


<div class="panel panel-default panel-badger">
  <div class="panel-body">
    <figure class="media">
      <div class="media-left">
        <a href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank" class="img-burn thumb-sm visible-lg">
          <img src="/bigbig-shark/images/avatar.jpg" class="img-rounded w-full" alt="">
        </a>
      </div>
      <div class="media-body">
        <h3 class="media-heading"><a href="https://tina-yao.gitee.io/bigbig-shark/" target="_blank"><span class="text-dark">大鲨鱼</span><small class="ml-1x">CV&amp;Robots</small></a></h3>
        <div>格物致知，诚意力行。</div>
      </div>
    </figure>
  </div>
</div>


    </div>
  </article>
  
    
  <section id="comments">
  	
      <div id="vcomments"></div>
    
  </section>


  
</div>

  <nav class="bar bar-footer clearfix" data-stick-bottom>
  <div class="bar-inner">
  
  <ul class="pager pull-left">
    
    <li class="prev">
      <a href="/bigbig-shark/2022/09/30/PASCAL-VOC%E6%95%B0%E6%8D%AE%E9%9B%86%E5%8F%8A%E8%87%AA%E5%B7%B1%E5%88%B6%E4%BD%9C%E6%95%B0%E6%8D%AE%E9%9B%86/" title="PASCAL VOC数据集及自己制作数据集"><i class="icon icon-angle-left" aria-hidden="true"></i><span>&nbsp;&nbsp;Newer</span></a>
    </li>
    
    
    <li class="next">
      <a href="/bigbig-shark/2022/09/27/Fully-Convolutional-Network/" title="Fully Convolutional Network"><span>Older&nbsp;&nbsp;</span><i class="icon icon-angle-right" aria-hidden="true"></i></a>
    </li>
    
    
  </ul>
  
  
  <!-- Button trigger modal -->
  <button type="button" class="btn btn-fancy btn-donate pop-onhover bg-gradient-warning" data-toggle="modal" data-target="#donateModal"><span>$</span></button>
  <!-- <div class="wave-icon wave-icon-danger btn-donate" data-toggle="modal" data-target="#donateModal">
    <div class="wave-circle"><span class="icon"><i class="icon icon-bill"></i></span></div>
  </div> -->
  
  
  <div class="bar-right">
    
    <div class="share-component" data-sites="weibo,qq,wechat" data-mobile-sites="weibo,qq"></div>
    
  </div>
  </div>
</nav>
  
<!-- Modal -->
<div class="modal modal-center modal-small modal-xs-full fade" id="donateModal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content donate">
      <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      <div class="modal-body">
        <div class="donate-box">
          <div class="donate-head">
            <p>Maybe you could buy me a cup of coffee.</p>
          </div>
          <div class="tab-content">
            <div role="tabpanel" class="tab-pane fade active in" id="alipay">
              <div class="donate-payimg">
                <img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/images/donate/alipayimg.PNG#images/donate/alipayimg.png" alt="Scan Qrcode" title="Scan" />
              </div>
              <p class="text-muted mv">Scan this qrcode</p>
              <p class="text-grey">Open alipay app scan this qrcode, buy me a coffee!</p>
            </div>
            <div role="tabpanel" class="tab-pane fade" id="wechatpay">
              <div class="donate-payimg">
                <img src="https://gitee.com/tina-yao/bigbig-shark/raw/master/images/donate/wechatpayimg.PNG#images/donate/wechatpayimg.png" alt="Scan Qrcode" title="Scan" />
              </div>
              <p class="text-muted mv">Scan this qrcode</p>
              <p class="text-grey">Open wechat app scan this qrcode, buy me a coffee!</p>
            </div>
          </div>
          <div class="donate-footer">
            <ul class="nav nav-tabs nav-justified" role="tablist">
              <li role="presentation" class="active">
                <a href="#alipay" id="alipay-tab" role="tab" data-toggle="tab" aria-controls="alipay" aria-expanded="true"><i class="icon icon-alipay"></i> alipay</a>
              </li>
              <li role="presentation" class="">
                <a href="#wechatpay" role="tab" id="wechatpay-tab" data-toggle="tab" aria-controls="wechatpay" aria-expanded="false"><i class="icon icon-wepay"></i> wechat payment</a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</div>



</main>

  <footer class="footer" itemscope itemtype="http://schema.org/WPFooter">
	
	
    <ul class="social-links">
    	
        <li><a href="https://gitee.com/tina-yao" target="_blank" title="Gitee" data-toggle=tooltip data-placement=top><i class="icon icon-gitee"></i></a></li>
        
    </ul>

    <div class="copyright">
    	
        &copy; 2023 BigbigShark
        
        <div class="publishby">
        <!--
        	Theme by <a href="https://github.com/cofess" target="_blank"> cofess </a>base on <a href="https://github.com/cofess/hexo-theme-pure" target="_blank">pure</a>.
        -->
        </div>
    </div>
</footer>
  <script src="//cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/jquery.min.js"><\/script>')
</script>

<script src="/bigbig-shark/js/plugin.min.js"></script>


<script src="/bigbig-shark/js/application.js"></script>


    <script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: 'Posts',
            PAGES: 'Pages',
            CATEGORIES: 'Categories',
            TAGS: 'Tags',
            UNTITLED: '(Untitled)',
        },
        ROOT_URL: '/bigbig-shark/',
        CONTENT_URL: '/bigbig-shark/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>

<script src="/bigbig-shark/js/insight.js"></script>






   




   
    
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//cdn.jsdelivr.net/npm/valine"></script>
  <script type="text/javascript">
  var GUEST = ['nick', 'mail', 'link'];
  var meta = 'nick,mail,link';
  meta = meta.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#vcomments',
    verify: false,
    notify: false,
    appId: '8pTCCvyyLGXskH0XW0fWHg7l-gzGzoHsz',
    appKey: 'id3rlCPRdbEoSpUfhJgcLhDO',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: meta,
    pageSize: '10' || 10,
    visitor: true
  });
  </script>

     







<script src="/bigbig-shark/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginModelPath":"assets/","model":{"jsonPath":"/bigbig-shark/live2dw/assets/tororo.model.json"},"display":{"position":null,"width":300,"height":600},"log":false,"pluginJsPath":"lib/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>